#pragma once
#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/time.h>
#include<atomic>
#include<fstream>
#include<vector>
#include<boost/algorithm/string.hpp>
namespace ns_util
{
    const std::string temp_path="./temp/";
    class PathUtil
    {
        public:
        static std::string AddSuffix(const std::string &filename,const std::string &suffix)
        {
            std::string path_name=temp_path;
            path_name+=filename;
            path_name+=suffix;
            return path_name;
        }

        static std::string Src(const std::string &filename)
        {
            return AddSuffix(filename,".cpp");
        }
        static std::string Exe(const std::string &filename)
        {
            return AddSuffix(filename,".exe");
        }
        static std::string CompilerError(const std::string &filename)
        {
            return AddSuffix(filename,".compile_error");
        }
        static std::string Stdin(const std::string &filename)
        {
            return AddSuffix(filename,".stdin");
        }
        static std::string Stdout(const std::string &filename)
        {
            return AddSuffix(filename,".stdout");
        }
        static std::string Stderr(const std::string &filename)
        {
            return AddSuffix(filename,".stderr");
        }

    };
        class TimeUtil
    {
        public:
        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time,nullptr);
            return std::to_string(_time.tv_sec);
        }

        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time,nullptr);
            return std::to_string(_time.tv_sec*1000+_time.tv_usec/1000);
        }


    };



    class FileUtil
    {
        public:
        static bool IsFileExists(const std::string &filename)
        {
            struct stat st;
            if(stat(filename.c_str(),&st)==0)
            {
                //属性获取成功
                return true;
            }
            return false;
        }

        static std::string UniqFileName()
        {
            std::atomic_uint id(0);
            id++;
            std::string ms=TimeUtil::GetTimeMs();
            std::string uniq_id=std::to_string(id);

            return ms+"_"+uniq_id;

        }
        static bool WriteFile(const std::string &target,const std::string &code)
        {
            std::ofstream out(target);
            if(!out.is_open())
            {
                return false;
            }
            out.write(code.c_str(),code.size());
            out.close();
            return true;

        }
        static bool ReadFile(const std::string&target,std::string *content,bool keep=false/**/)
        {
            (*content).clear();
            std::ifstream in(target);
            if(!in.is_open())
            {
                return  false;
            }
            std::string line;
            //getline内部重载了强制类型转换
            //getline不保存分隔符，有时需要保留\n
            while(std::getline(in,line))
            {
                (*content)+=line;
                (*content)+=(keep?"\n":"");
            }
            return true;
        }

    };
    class StringUtil
    {
        public:
        /**
         * str输入型  要切分的字符串
         * target 输出型 保存切分的结果
        */
        static void SplitString(std::string &str,std::vector<std::string> *target,std::string sep)
        {
            boost::split((*target),str,boost::algorithm::is_any_of(sep),boost::algorithm::token_compress_on);
        }

    };

}
